/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.mobiubiq.directory.client.data;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;

/**
 *
 * @author Marnix Renne
 */
public class ServiceSettingsRecordStore {
    private static final String RECORD_STORE_ID = "service-settings";
    
    public static Vector getSettings(Service service) {
        RecordStore recordStore = null;
        try {
            recordStore = RecordStore.openRecordStore(RECORD_STORE_ID, true);
            RecordEnumeration re = recordStore.enumerateRecords(null, null, false);
            while (re.hasNextElement()) {
                DataInputStream dataInputStream = new DataInputStream(new ByteArrayInputStream(re.nextRecord()));
                if (service.getId().equals(dataInputStream.readUTF())) {
                    Vector result = new Vector();
                    while (dataInputStream.available() > 0) {
                        String name = dataInputStream.readUTF();
                        String value = dataInputStream.readUTF();
                        Setting setting = new Setting();
                        setting.setName(name);
                        setting.setValue(value);
                        result.addElement(setting);
                        //setValue(settings, name, value);
                    }
                    return result;
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        } finally {
            if(recordStore != null) {
                try {recordStore.closeRecordStore();} catch (RecordStoreException ex) {}
            }
        }
        return null;
    }
    
    public static void setSettings(Service service) {
        RecordStore recordStore = null;
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
            dataOutputStream.writeUTF(service.getId());
            Enumeration settings = service.getSettings().elements();
            while (settings.hasMoreElements()) {
                Setting setting = (Setting) settings.nextElement();
                
                dataOutputStream.writeUTF(setting.getName());
                dataOutputStream.writeUTF(setting.getValue());
            }
            byte[] data = byteArrayOutputStream.toByteArray();
            recordStore = RecordStore.openRecordStore(RECORD_STORE_ID, true);
            recordStore.addRecord(data, 0, data.length);
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if(recordStore != null) {
                try {recordStore.closeRecordStore();} catch (RecordStoreException ex) {}
            }
        }
    }

    public static void removeSettings(Service service) {
        removeSettings(service.getId());
    }
    
    public static void removeSettings(String serviceId) {
        RecordStore recordStore = null;
        try {
            recordStore = RecordStore.openRecordStore(RECORD_STORE_ID, true);
            RecordEnumeration re = recordStore.enumerateRecords(null, null, false);
            while (re.hasNextElement()) {
                int recordId = re.nextRecordId();
                
                DataInputStream dataInputStream = new DataInputStream(new ByteArrayInputStream(recordStore.getRecord(recordId)));
                if (serviceId.equals(dataInputStream.readUTF())) {
                    recordStore.deleteRecord(recordId);
//                    Enumeration settings = service.getSettings().elements();
//                    while(settings.hasMoreElements()) {
//                        Setting setting = (Setting) settings.nextElement();
//                        setting.setValue(null);
//                    }
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        } finally {
            if(recordStore != null) {
                try {recordStore.closeRecordStore();} catch (RecordStoreException ex) {}
            }            
        }
    }

    public static Vector getServiceIds() {
        Vector result = new Vector();
        RecordStore recordStore = null;
        try {
            recordStore = RecordStore.openRecordStore(RECORD_STORE_ID, true);
            RecordEnumeration re = recordStore.enumerateRecords(null, null, false);
            while (re.hasNextElement()) {
                DataInputStream dataInputStream = new DataInputStream(new ByteArrayInputStream(re.nextRecord()));
                result.addElement(dataInputStream.readUTF());
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        } finally {
            if(recordStore != null) {
                try {recordStore.closeRecordStore();} catch (RecordStoreException ex) {}
            }            
        }
        return result;
    }
    
    private static void setValue(Vector settings, String name, String value) {
        Enumeration e = settings.elements();
        while(e.hasMoreElements()) {
            Setting setting = (Setting) e.nextElement();
            if(setting.getName().equals(name)) {
                setting.setValue(value);
            }
        }
    }
}
